package com.leo.sunnyweather.logic

import android.util.Log
import androidx.lifecycle.liveData
import com.leo.sunnyweather.logic.model.Place
import com.leo.sunnyweather.logic.network.SunnyWeatherNetWork
import kotlinx.coroutines.Dispatchers

/**
 * Author: Leo Liao <lliao@midtronics.com>
 * Date: 01/02/2024 17:25
 * Description:
 */
object Repository {

    fun searchPlaces(query: String) =
        liveData(Dispatchers.IO) {
            var result = try {
                val response = SunnyWeatherNetWork.getPlaceByName(query)
                Log.e("Leo",response.toString())
                if (response.status == "ok") {
                    Result.success(response.places)
                } else {
                    Result.failure(Exception("Response status is ${response.status}"))
                }
            } catch (e: Exception) {
                Result.failure(e)
            }
            emit(result)
        }
}